package com.atguigu.yygh.user.api;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.model.user.UserInfo;
import com.atguigu.yygh.user.service.UserInfoService;
import com.atguigu.yygh.user.utils.AuthContextHolder;
import com.atguigu.yygh.vo.user.LoginVo;
import com.atguigu.yygh.vo.user.UserAuthVo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;
@Api(tags = "用户登录")
@RestController
@RequestMapping("/api/user")
@Slf4j
public class UserInfoApiController {
    @Autowired
    private UserInfoService userInfoService;
    //抽取类LoginVo 用于登登录
    //用户手机号登录
    @PostMapping("login")
    public Result login(@RequestBody LoginVo loginVo){
        log.info("传递得信息{}",loginVo);
        //返回信息过多使用map
        Map<String,Object> map=userInfoService.loginUser(loginVo);
        return Result.ok(map);
    }
    //用户认证
    @PostMapping("auth/userAuth")
    //UserAuthVo用户认证的Vo类
    public Result auth(@RequestBody UserAuthVo userAuthVo, HttpServletRequest request){
        //传递俩参数 id和Vo对象
        userInfoService.auth(AuthContextHolder.getUserId(request),userAuthVo);
        return Result.ok();

    }
    //获取用户id信息接口
    @GetMapping("auth/getUserInfo")
    public Result getUserInfo(HttpServletRequest request) {
        Long userId = AuthContextHolder.getUserId(request);
        UserInfo userInfo = userInfoService.getById(userId);
        return Result.ok(userInfo);
    }
}
